package com.alibaba.springbootrbac.controller;

import com.alibaba.springbootrbac.model.entity.Permission;
import com.alibaba.springbootrbac.model.param.permission.PermissionCreateParam;
import com.alibaba.springbootrbac.model.param.permission.PermissionPageQueryParam;
import com.alibaba.springbootrbac.model.param.permission.PermissionUpdateParam;
import com.alibaba.springbootrbac.model.response.permission.PermissionVO;
import com.alibaba.springbootrbac.service.PermissionService;
import com.alibaba.springbootrbac.utils.PermissionUtils;
import com.alibaba.springbootrbac.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZhouYaDong
 * @date 2025/8/23
 * @description 权限控制器
 */
@RestController
@RequestMapping("/permissions")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    /**
     * 获取枚举中的所有权限列表-用于调试
     * @return 权限列表
     */
    @GetMapping("/getAllPermissions")
    public Result<List<PermissionVO>> getAllPermissions(){
        List<PermissionVO> permissionVOS = PermissionUtils.listAllPermissions();
        return Result.success(permissionVOS);
    }

    @PostMapping("/createPermission")
    public Result<String> createPermission(@RequestBody @Validated PermissionCreateParam param) {
        permissionService.addPermission(param);
        return Result.success("权限创建成功");
    }

    @PostMapping("/updatePermissionById")
    public Result<String> updatePermission(@RequestBody @Validated PermissionUpdateParam param) {
        permissionService.updatePermission(param);
        return Result.success("权限更新成功");
    }

    @PostMapping("/deletePermissionById")
    public Result<String> deletePermission(@RequestParam("id") @NotBlank String id) {
        permissionService.deletePermission(id);
        return Result.success("权限删除成功");
    }

    @PostMapping("/pageList")
    public Result<IPage<Permission>> getPermissionPageList(@RequestBody @Validated PermissionPageQueryParam param) {
        return Result.success(permissionService.getPermissionPageList(param));
    }
}
